<script type="text/javascript">
	//表格查询  
	function reloadReturnOrder() {
		var params = returnOrderList.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		returnOrderList.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		returnOrderList.datagrid('unselectAll');
		returnOrderList.datagrid('uncheckAll');
	}


	
	function editReturnOrder() {
		var rows = returnOrderList.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindowOptions({
				title:"编辑信息",
				maximizable:true,
				width:1050,
				height:600,
				href:"order.do?editOrder&orderId=" + rows[0].id,
				callback:function(d) {
					var data = d.form("getData");
					var itemDatas = $('#orderItem_list').datagrid('getRows');
					if(itemDatas.length==0) {
						showMsg('请添加单据内容');
					}else {
						data.items = itemDatas;
						 $.post('order.do?createOrder',{data:JSON.stringify(data)},function(result){
			                    if (result.success){
			                    	reloadOrder();    
			                    }
			                    showMsg(result.msg);
			                },'json');
					}
				},
				enableSaveButton:rows[0].status=='1'
			});
		} else {
			showMsg('请选择操作记录.');
		}

	}
	
	
	
	
	function clearReturnOrderSearch() {
		setGridSearchParams('returnOrderList','returnOrderSearchForm',true,reloadReturnOrder);
	}
	
	//根据条件查询
	function returnOrderSearch() {
		setGridSearchParams('returnOrderList','returnOrderSearchForm',false,reloadReturnOrder);
	}
	
	function printReturnOrder() {
		var rows = returnOrderList.datagrid('getChecked');
	    if (rows&&rows.length>0){
	    	if(rows.length>1) {
				showMsg('请选择一条订单进行打印.');
				return;
			}
           	window.open('order.do?print&orderId='+rows[0].id, '_blank');
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	
	
	function onDblClickOrderRow(rowIndex, rowData) {
		createFromWindowOptions({
			title:"编辑信息",
			maximizable:true,
			width:1050,
			height:600,
			href:"order.do?editOrder&orderId=" + rowData.id,
			callback:function(d) {
				var data = d.form("getData");
				var itemDatas = $('#orderItem_list').datagrid('getRows');
				if(itemDatas.length==0) {
					showMsg('请添加单据内容');
				}else {
					data.items = itemDatas;
					 $.post('order.do?createOrder',{data:JSON.stringify(data)},function(result){
		                    if (result.success){
		                    	reloadOrder();    
		                    }
		                    showMsg(result.msg);
		                },'json');
				}
			},
			enableSaveButton:rowData.status=='1'
		});
	}
	
	function deleteReturnOrder() {
		var rows = returnOrderList.datagrid('getChecked');
	    if (rows&&rows.length>0){
	    	for(var i=0; i<rows.length; i++){
        		if(rows[0].status!='7') {
    				showMsg(rows[0].no+'为非退单待审核状态，无法删除，请确认订单状态!',2500);
    				return;
    			}
        		
        	}
	    	
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('order.do?deleteOrderItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadReturnOrder();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	
	function returnOrderPass(actionStatus) {
		var rows = returnOrderList.datagrid('getChecked');
		if (rows && rows.length > 0) {
			var ids = "";
			for ( var i = 0; i < rows.length; i++) {
				//非待退货审核，
// 				printLog(rows[i]);
				if (rows[i].status != '7'&&actionStatus==5) {
					showMsg(rows[i].no + '为非【待退单审核】状态的订单，无法进行审核，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '5'&&actionStatus==8) {
					showMsg(rows[i].no + '为非【待收货确认】状态的订单，无法进行确认，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '8'&&actionStatus==9) {
					showMsg(rows[i].no + '为非【待退款确认】状态的订单，无法进行确认，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '9'&&actionStatus==6) {
					showMsg(rows[i].no + '为非【待收款确认】状态的订单，无法进行确认，请确认订单状态！', 2500);
					return;
				}
				ids += rows[i].id + ",";
			}
			$.post('order.do?checkReturnPass', {
				ids : ids,
				actionStatus:actionStatus
			}, function(result) {
				if (result.success) {
					reloadReturnOrder();
				}
				showMsg(result.msg);
			}, 'json');
		} else {
			showMsg("请选择操作记录.");
		}
	}
</script>

<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false">
		
		<@eu.datagrid id="returnOrderList" toolbar="#returnOrderListSearch" idField="id" 
			 url="order.do?loadList&orderType=return" rownumbers="true" onDblClickRow="onDblClickOrderRow(rowIndex, rowData)"
			 item="com.xdtech.ad.vo.OrderItem"/>
		
		<div id="returnOrderListSearch">
			<div style="margin-bottom:5px;padding-left:5px;margin-top: 5px;">
				<form id="returnOrderSearchForm">
					<!-- 防止form表单里只有一个text元素时候，按enter键自动提交问题 -->
					<input style="display:none" />
					<input id="filterComplete" name="filter" type="hidden" />
					<table>
						<tr>
							<td>名称：</td>
							<td><input class="easyui-validatebox textbox" name="name" /></td>
							<td>
								<@eu.linkbutton showName="查询" iconCls="icon-search" onclick="returnOrderSearch()"/>
	 					 		<@eu.linkbutton showName="清空" iconCls="icon-clear" onclick="clearReturnOrderSearch()"/>
 					 		</td>
						</tr>
					</table>
				</form>
				
			</div>
			<div id="returnOrderToolbar" style="height: auto">
				<@eu.linkbutton showName="删除" shiro="ad:returnOrder:delete" iconCls="icon-remove" onclick="deleteReturnOrder()"/>
				<@eu.linkbutton showName="审核通过" shiro="ad:returnOrder:returnOrderPass" iconCls="icon-edit" onclick="returnOrderPass(5)"/>
				<@eu.linkbutton showName="收货确认" shiro="ad:returnOrder:receiveReturnOrdePass" iconCls="icon-edit" onclick="returnOrderPass(8)"/>
				<@eu.linkbutton showName="退款确认" shiro="ad:returnOrder:refundReturnOrdePass" iconCls="icon-edit" onclick="returnOrderPass(9)"/>
				<@eu.linkbutton showName="收款确认" shiro="ad:returnOrder:paymentReturnOrdePass" iconCls="icon-edit" onclick="returnOrderPass(6)"/>
				
				<@eu.linkbutton showName="打印" shiro="ad:returnOrder:printOrder" iconCls="icon-edit" onclick="printReturnOrder()"/>
				<@eu.linkbutton showName="刷新" shiro="ad:returnOrder:refresh" iconCls="icon-reload" onclick="reloadReturnOrder()"/>
			</div>
		</div>
	</div>
</div>
<script>
	if(window.tabsFilter) {
		$('#filterComplete').val(window.tabsFilter);
		setTimeout(function() {returnOrderSearch();$('#filterComplete').val('');},200);
	}
	window.tabsFilter = undefined;
</script>